from sanic import response

from model.deviceModel import search_dev_param, search_user_link_dev, update_dev_param
from model.salesmanModel import judge_a_staff
from model.userModel import select



async def device(request):
        device_id = request.form.get('device_id')
        # 设备参数
        param = search_dev_param(device_id=device_id)
        # 操作人员
        users_dev = search_user_link_dev(device_id=device_id)  # 所有与这台设备有关的人
        devUser_list = [users_dev[i]['user_id'] for i in range(len(users_dev))]
        operator = None
        for user in devUser_list:
                he = judge_a_staff(user_id=user, role_id=4)  # 4-D-饲养员
                # <<<<<<<<<<只有一个操作人员吗？
                if he:
                        operator = select(id=user)['username']

        return response.json(param)


async def edit_dev_config(request):     # <<<<<<<<<<未完成
        device_id = request.form.get('device_id')

        feed_time = request.form.get('feed_time')
        model = request.form.get('model')       # 模式状态
        trig_num = request.form.get('trig_num')         # 触发料量
        trig_interval = request.form.get('trig_interval')       # 触发间隔
        wat_fer_rate = request.form.get('wat_fer_rate')         # 水料比

        new_param = {'trig_num': trig_num,
                     'trig_interval': trig_interval,
                     'wat_fer_rate': wat_fer_rate,
                     'pow_par': model}

        update_dev_param(device_id=device_id, **new_param)





